package com.yunxi.service.dubboService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.BeanTools;
import com.yunxi.model.sto.StoFrozenReason;
import com.yunxi.service.localService.CodeCreateService;
import com.yunxi.service.localService.sto.StoFrozenReasonService;
import com.yunxi.sto.pojo.StoFrozenReasonVO;
import com.yunxi.sto.service.StoFrozenReasonDubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

/**
 * <p>
 * 车辆冻结原因 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-21
 */
public class StoFrozenReasonDubboServiceImpl implements StoFrozenReasonDubboService {

    @Autowired
    @Qualifier(value = "stoFrozenReasonServiceImpl")
    StoFrozenReasonService stoFrozenReasonService;

    @Autowired
    @Qualifier(value = "codeCreateServiceImpl")
    CodeCreateService codeCreateService;

    @Override
    public StoFrozenReasonVO getById(String id) {
        StoFrozenReason stoFrozenReason = stoFrozenReasonService.getById(id);
        StoFrozenReasonVO stoFrozenReasonVO = new StoFrozenReasonVO();
        BeanTools.copyProperties(stoFrozenReasonVO,stoFrozenReason);
        return stoFrozenReasonVO;
    }

    @Override
    public void saveOrUpdate(StoFrozenReasonVO stoFrozenReasonVO)throws Exception  {
        StoFrozenReason stoFrozenReason = new StoFrozenReason();
        BeanTools.copyProperties(stoFrozenReason,stoFrozenReasonVO);
        if(stoFrozenReason.getFrozenReasonId() == null){
            //新增冻结编号
            String fr = codeCreateService.createCode("FR", 4);
            stoFrozenReason.setFrozenReasonNo(fr);
        }
        //校验key不可重复
        //校验系统级不可编辑
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("FROZEN_REASON_KEY",stoFrozenReasonVO.getFrozenReasonKey());
        if(stoFrozenReasonVO.getFrozenReasonId() != null){
            queryWrapper.ne("FROZEN_REASON_ID",stoFrozenReasonVO.getFrozenReasonId());
            if(stoFrozenReasonVO.getIsSystemReason() != null && stoFrozenReasonVO.getIsSystemReason().equals(1)){
                throw new Exception("操作失败,系统级的冻结原因不可编辑!");
            }
        }
        List list = stoFrozenReasonService.list(queryWrapper);
        if(list != null && list.size() > 0){
            throw new Exception("操作失败,冻结原因KEY不可重复!");
        }
        stoFrozenReasonService.saveOrUpdate(stoFrozenReason);
    }

    @Override
    public void removeById(String id) {
    stoFrozenReasonService.removeById(id);
    }

    @Override
    public void delAndReset(List<String> list,Integer isDel,String userId) {
        stoFrozenReasonService.delAndReset(list,isDel,userId);
    }

    @Override
    public List reasonList() {
        return stoFrozenReasonService.reasonList();
    }

    @Override
    public PageInfo<StoFrozenReasonVO> findPageByVO(StoFrozenReasonVO stoFrozenReasonVO, Integer pageNum, Integer pageSize){
        PageInfo<StoFrozenReasonVO> pageInfo = stoFrozenReasonService.pageByVo(stoFrozenReasonVO, pageNum, pageSize);
        return pageInfo;
    }

}
